package com.zenithmind.chat.controller;

import com.zenithmind.chat.pojo.vo.ChatFriendRequestVO;
import com.zenithmind.chat.service.ChatFriendRequestService;
import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 好友申请管理控制器
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Tag(name = "好友申请管理", description = "好友申请相关接口")
@Slf4j
@RestController
@RequestMapping("/api/zenithMind/chat/friend-request")
@RequiredArgsConstructor
public class ChatFriendRequestController {

    private final ChatFriendRequestService friendRequestService;

    @Operation(summary = "分页查询好友申请列表")
    @GetMapping("/page")
    public Result<PageResult<ChatFriendRequestVO>> getFriendRequestPage(
            @Parameter(description = "申请类型") @RequestParam(required = false) String type,
            @Parameter(description = "申请状态") @RequestParam(required = false) Integer status,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "页大小") @RequestParam(defaultValue = "10") Integer pageSize) {
        
        String userId = UserContext.getUserId();
        PageResult<ChatFriendRequestVO> result = friendRequestService.getFriendRequestPage(
            userId, type, status, pageNum, pageSize);
        
        return Result.success(result);
    }

    @Operation(summary = "查询用户好友申请列表")
    @GetMapping("/list")
    public Result<List<ChatFriendRequestVO>> getUserFriendRequests(
            @Parameter(description = "申请类型") @RequestParam(required = false) String type,
            @Parameter(description = "申请状态") @RequestParam(required = false) Integer status) {
        
        String userId = UserContext.getUserId();
        List<ChatFriendRequestVO> result = friendRequestService.getUserFriendRequests(userId, type, status);
        
        return Result.success(result);
    }

    @Operation(summary = "根据ID查询好友申请详情")
    @GetMapping("/{id}")
    public Result<ChatFriendRequestVO> getFriendRequestById(
            @Parameter(description = "申请ID") @PathVariable String id) {
        
        String userId = UserContext.getUserId();
        ChatFriendRequestVO result = friendRequestService.getFriendRequestById(id, userId);
        
        return Result.success(result);
    }

    @Operation(summary = "处理好友申请")
    @PostMapping("/{id}/handle")
    public Result<Boolean> handleFriendRequest(
            @Parameter(description = "申请ID") @PathVariable String id,
            @Parameter(description = "是否同意") @RequestParam Boolean accept,
            @Parameter(description = "回复消息") @RequestParam(required = false) String responseMessage) {
        
        String userId = UserContext.getUserId();
        Boolean result = friendRequestService.handleFriendRequest(id, accept, responseMessage, userId);
        
        return Result.success(result);
    }

    @Operation(summary = "撤回好友申请")
    @PostMapping("/{id}/withdraw")
    public Result<Boolean> withdrawFriendRequest(
            @Parameter(description = "申请ID") @PathVariable String id) {
        
        String userId = UserContext.getUserId();
        Boolean result = friendRequestService.withdrawFriendRequest(id, userId);
        
        return Result.success(result);
    }

    @Operation(summary = "删除好友申请记录")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteFriendRequest(
            @Parameter(description = "申请ID") @PathVariable String id) {
        
        String userId = UserContext.getUserId();
        Boolean result = friendRequestService.deleteFriendRequest(id, userId);
        
        return Result.success(result);
    }

    @Operation(summary = "批量处理好友申请")
    @PostMapping("/batch-handle")
    public Result<Boolean> batchHandleFriendRequests(
            @Parameter(description = "申请ID列表") @RequestBody List<String> requestIds,
            @Parameter(description = "是否同意") @RequestParam Boolean accept) {
        
        String userId = UserContext.getUserId();
        Boolean result = friendRequestService.batchHandleFriendRequests(requestIds, accept, userId);
        
        return Result.success(result);
    }

    @Operation(summary = "统计好友申请数量")
    @GetMapping("/count")
    public Result<Integer> countFriendRequests(
            @Parameter(description = "申请类型") @RequestParam(required = false) String type,
            @Parameter(description = "申请状态") @RequestParam(required = false) Integer status) {
        
        String userId = UserContext.getUserId();
        Integer result = friendRequestService.countFriendRequests(userId, type, status);
        
        return Result.success(result);
    }

    @Operation(summary = "检查是否已发送过申请")
    @GetMapping("/check-exists")
    public Result<Boolean> checkRequestExists(
            @Parameter(description = "目标用户ID") @RequestParam String targetId) {
        
        String userId = UserContext.getUserId();
        Boolean result = friendRequestService.checkRequestExists(userId, targetId);
        
        return Result.success(result);
    }

    @Operation(summary = "清理过期申请")
    @PostMapping("/clean-expired")
    public Result<Integer> cleanExpiredRequests(
            @Parameter(description = "过期天数") @RequestParam(defaultValue = "7") Integer days) {
        
        Integer result = friendRequestService.cleanExpiredRequests(days);
        
        return Result.success(result);
    }

    @Operation(summary = "获取申请统计信息")
    @GetMapping("/statistics")
    public Result<Object> getRequestStatistics(
            @Parameter(description = "开始时间") @RequestParam(required = false) String startTime,
            @Parameter(description = "结束时间") @RequestParam(required = false) String endTime) {
        
        String userId = UserContext.getUserId();
        Object result = friendRequestService.getRequestStatistics(userId, startTime, endTime);
        
        return Result.success(result);
    }
}
